<?php

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 *  主播推送日志
 */
class HostPushLog extends Model
{
    protected $table = 'host_push_log';
    protected $fillable = ['host_id', 'receive_id', 'flag', 'push_type', 'batch_no'];

    //推送类型-系统三件套
    const TYPE_SYS_PACK = 1;
    //主动搭讪
    const TYPE_HOST_GREET = 2;
    //缘遇
    const TYPE_YUANYU = 3;

    /**
     * 日志添加
     */
    static function addLog(
        $hostId = 0,
        $userId = 0,
        $pushType = self::TYPE_HOST_GREET,
        $batchNo = 1
    ) {
        return self::create([
            'host_id' => $hostId,
            'receive_id' => $userId,
            'push_type' => $pushType,
            'batch_no' => $batchNo,
            'flag' => 1
        ]);
    }

    /**
     * 获取推送过的主播ID
     */
    static function getPushHostIds($receiveId = 0, $offTime = 0)
    {
        $where = [
            ['receive_id', $receiveId]
        ];
        $offTime && $where[] = ['created_at', '>', date('Y-m-d H:i', time() - $offTime)];
        return self::where($where)->pluck('host_id')->toArray();
    }

    /**
     * 获取推送过的用户ID
     */
    static function getPushUserIds($hostId = 0, $offTime = 0)
    {
        $where = [
            ['host_id', $hostId]
        ];
        $offTime && $where[] = ['created_at', '>', date('Y-m-d H:i', time() - $offTime)];
        return self::where($where)->pluck('receive_id')->toArray();
    }

    /**
     * 用户是否被推送
     */
    static function checkPushedCount($receiveId = 0, $offTime = 0, $hostId = 0)
    {
        $where = [
            ['receive_id', $receiveId]
        ];
        $hostId && $where[] = ['host_id', $hostId];
        $offTime && $where[] = ['created_at', '>', date('Y-m-d H:i', time() - $offTime)];
        return self::where($where)->count();
    }
}
